package cn.bestick.cblog.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dmo.Category;
import cn.bestick.cblog.dto.CategoryDto;
import cn.bestick.cblog.service.CategoryService;
import cn.bestick.cblog.service.impl.CategoryServiceImpl;
import cn.bestick.cblog.util.StringUtil;

public class CategoryManageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private CategoryService categoryService = new CategoryServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");

		if ("preSave".equals(action)) {
			preSave(request, response);
		} else if ("save".equals(action)) {
			save(request, response);
		} else if ("delete".equals(action)) {
			delete(request, response);
		} else {
			list(request, response);
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<CategoryDto> categories = categoryService.getCategories();
		request.setAttribute("categories", categories);
		request.setAttribute("mainPage", "template/category/manage-list.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void preSave(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String categoryId = request.getParameter("categoryId");
		if (StringUtil.isNotEmpty(categoryId)) {
			Category category = categoryService.getCategory(Integer
					.parseInt(categoryId));
			request.setAttribute("category", category);
		}
		request.setAttribute("mainPage", "template/category/manage-save.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String categoryId = request.getParameter("categoryId");
		String categoryName = request.getParameter("categoryName");
		Category category = new Category();
		category.setName(categoryName);
		if (StringUtil.isNotEmpty(categoryId)) {
			category.setId(Integer.parseInt(categoryId));
		}
		if (StringUtil.isNotEmpty(categoryId)) {
			categoryService.updateCategory(category);
		} else {
			categoryService.saveCategory(category);
		}
		request.getRequestDispatcher("categoryManage?action=list").forward(
				request, response);

	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String categoryId = request.getParameter("categoryId");
		
		if (categoryService.exist(Integer.parseInt(categoryId))) {
			request.setAttribute("error", "此分类下尚有博文，不可删除");
		} else {
			categoryService.deleteCategory(Integer.parseInt(categoryId));
		}
		request.getRequestDispatcher("categoryManage?action=list").forward(
				request, response);
	}

}
